import numpy as np
import librosa
from keras.models import load_model

data_path = '/home/orient/文档/voiceprint_test_a/data/'

def get_mfcc(wav_file):
    wave, sr = librosa.load(data_path+wav_file, sr=None)
    mfcc = librosa.feature.mfcc(wave, sr, n_mfcc=13)
    delta_mfcc = librosa.feature.delta(mfcc)
    delta2_mfcc = librosa.feature.delta(mfcc, order=2)
    feature = np.vstack([mfcc, delta_mfcc, delta2_mfcc])

    # reshape feature
    feature = mfcc_reshape(feature)

    return feature

def mfcc_reshape(*feature):
    mfcc = feature[0]
    zeros = np.zeros((mfcc.shape[0], 1))

    # slice
    while (mfcc.shape[1] < 100):
        mfcc = np.column_stack((mfcc, zeros))
    # intercept
    if (mfcc.shape[1] >= 100):
        mfcc = mfcc[:, :100]

    # Normalization
    mfcc = mfcc.reshape(mfcc.shape[0], -1) / np.max(mfcc)

    return mfcc


model = load_model('model/model1.h5')

mfcc1 = get_mfcc('79406a89f249d1e5753786ce9140c52b.wav')
mfcc2 = get_mfcc('51dfeeb6311d2afc0dcc12a89500ffc5.wav')
input = np.row_stack((mfcc1, mfcc2))
input = input.reshape(1, 7800)
res = model.predict(input)
print(res)
